//
//  LearningToast.swift
//  LearningLanguage
//
//  Created by Rae on 2023/9/7.
//

import UIKit
import SPIndicator
import SwiftEntryKit

public let ToastTool = FJToastTool.default

public class FJToastTool: NSObject, AnimationProtocol {
    
    static let `default` = FJToastTool()

    private func modifyToastStyle() {
        SPIndicatorView.appearance().tintColor = "#7848F5".toColor
        SPIndicatorView.appearance().duration = 2.0
        SPIndicatorView.appearance().backgroundColor = .black
    }
    
    func show() {
        DispatchQueue.main.async {
            let loadingView = c_make(AIGirlLoadingView.self)
            self.animation.animation(show: loadingView)
        }
    }
    func dismiss() {
        DispatchQueue.main.async {
            self.animation.animation(dismiss: AIGirlLoadingView())
        }
    }
    
    func dismiss(delay: Int) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: UInt64(delay))) {
            self.animation.animation(dismiss: AIGirlLoadingView())
        }
    }
    
    func toast(_ message: String, status: SPIndicatorHaptic) {
        modifyToastStyle()
        dismiss()
        if status == .none || status == .warning {
            let view = SPIndicatorView(title: message, message: nil)
            view.titleLabel?.font = 15.font_bold
            view.titleLabel?.adjustsFontSizeToFitWidth = true
            view.present(duration: 1.5, haptic: status, completion: nil)
        } else {
            let preset: SPIndicatorIconPreset = status == .success ? .done : .error
            let view = SPIndicatorView(title: message, message: nil, preset: preset)
            view.titleLabel?.font = 15.font_bold
            view.titleLabel?.adjustsFontSizeToFitWidth = true
            view.present(duration: 1.5, haptic: status, completion: nil)
        }
    }
}

extension String {
    
    //TODO: - 成功提示
    var toast_success: Void {
        DispatchQueue.main.async {
            ToastTool.toast(self, status: .success)
        }
        return
    }
    
    //TODO: - 错误提示
    var toast_error: Void {
        DispatchQueue.main.async {
            ToastTool.toast(self, status: .error)
        }
        return
    }
    
    //TODO: - 警告
    var toast_warning: Void {
        DispatchQueue.main.async {
            ToastTool.toast(self, status: .warning)
        }
        return
    }
    
}
